package com.usian.controller;

import com.usian.pojo.Student;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;

@Controller
@RequestMapping("upload")
public class UploadController {


    @RequestMapping("/importExcel")
    public void importExcel(MultipartFile files){
        //获取文件
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(files.getInputStream());
            //获取工作表
            HSSFSheet sheet = workbook.getSheetAt(0);
            //获取到总行数
            int lastRowNum = sheet.getLastRowNum();
            //遍历
            for(int i=0;i<=lastRowNum;i++){
                //获取到每一行的数据
                HSSFRow row = sheet.getRow(i);
                //获取每一行内容
                String  name = row.getCell(0).getStringCellValue();
                String address = row.getCell(1).getStringCellValue();
                Student student = new Student();
                String sex = row.getCell(2).getStringCellValue();
                if (sex.equals("男")) {
                    student.setSex("0");
                }else{
                    student.setSex("1");
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                student.setHiredate(simpleDateFormat.format(date));//2012-12-12

                student.setEname(name);
                student.setAddress(address);
                studentService.saveStudent(student);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导入Excel错误",e);
        }
        //使用poi进行遍历
        //遍历excel里的数据
        //写入到mysql数据库
    }
}
